#!/bin/bash
# Copyright (c) 2022-2023 Chengxuya.
# This is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#
#          http://license.coscl.org.cn/MulanPSL2
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

print_volume() {
    readonly muted="$(amixer get Master | tail -n1 | sed -r 's/.*\[(.*)].*/\1/')"
    readonly volume="$(amixer get Master | tail -n1 | sed -r 's/.*\[(.*)%\].*/\1/')"
    if [ $muted = "on" ] && [ $volume -gt 0 ]; then
        echo -e "音量:${volume}%"
    else
        echo -e "音量: Mute"
    fi
}

print_memory() {
    readonly memUsed=$(free -h | awk '(NR == 2) {print $3}')
    echo -e "内存:$memUsed"
}

print_cpu_temperature() {
    readonly temperature=$(sensors | grep 'id' | awk '{printf $4}' | sed 's/\+//')
    echo -e "CPU温度:$temperature"
}

print_date() {
    date '+%Y/%m/%d ⏰%H:%M'
}

print_cpu() {
    readonly cpu=$(sar -u 1 1 | tail -n1)
    readonly cpuUser=$(echo $cpu | awk '{printf $3}')
    readonly cpuNice=$(echo $cpu | awk '{printf $4}')
    readonly cpuSystem=$(echo $cpu | awk '{printf $5}')
    echo -e "CPU:U$cpuUser% N$cpuNice% S$cpuSystem%"
}

xsetroot -name " 程序鸭呀 | $(print_cpu_temperature) $(print_volume) | $(print_memory) | $(print_cpu) | $(print_date) "
